/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.reminder;

import org.niclab.vanbus.activity.comp.EndStopReminderDialog;
import org.niclab.vanbus.activity.controller.BusStopController;
import org.niclab.vanbus.activity.controller.ReminderController;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;

public class EndStopReminderReceiver extends BroadcastReceiver{
	
	
	private static final String LOG_TAG="EndStopReminderReceiver"; 



	
	@Override
	public void onReceive(Context context, Intent intent) {
		WakefulService.acquireStaticLock(context);
		
		int intentSrc = intent.getIntExtra(ReminderController.REMINDER_END_STOP_LOCATION_UPDATE_INTENT_KEY_SOURCE, 0);
		LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		
		Intent tempIntent = new Intent(intent.getAction());
		tempIntent.setData(intent.getData());
		
		PendingIntent origPendingIntent = PendingIntent.getBroadcast(context,0, tempIntent, 0);
		Log.d(LOG_TAG, "PendingIntent to remove: " + origPendingIntent);
		Log.v(LOG_TAG, "Pending to remove hashcode: " + origPendingIntent.hashCode());
		if (intentSrc == ReminderController.REMINDER_END_STOP_INTENT_SRC_PROXIMITY_ALERT){
			// cancel the proximity alert, we don't need it anymore
			
			locManager.removeProximityAlert(origPendingIntent);
			
			
		}else{
			// cancel the last location update
			
			locManager.removeUpdates(origPendingIntent);
		}
		Log.d(LOG_TAG, "intent's action: " + intent.getAction());
		Log.d(LOG_TAG, "intent's source: " + intent.getIntExtra(ReminderController.REMINDER_END_STOP_LOCATION_UPDATE_INTENT_KEY_SOURCE,0));
		intent.setClass(context, EndStopReminderService.class);
		context.startService(intent);
	
		
		
	}

}
